package com.eastcom.groovy

import com.eastcom.groovy.config.ServerPath
import com.eastcom.groovy.context.ServerContext
import com.eastcom.groovy.process.ServerDestroyProcess;
import com.eastcom.groovy.process.ServerInitProcess;
import com.eastcom.groovy.provider.PropertyPlaceholderConfigurerProvider;
import com.eastcom.groovy.provider.SpringContextProvider
import com.eastcom.groovy.tools.file.JaxbUtil;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class Launcher extends Application {
	private static Log logger = LogFactory.getLog(Launcher.class);
	private boolean flag = true
	protected void mainloop() {
		logger.info(" mystope程序开始启动。。。 ");
		ServerInitProcess serverInitProcess = SpringContextProvider.getBean("serverInitProcess");
		serverInitProcess.init();
		logger.debug("服务初始化完成！开启监听套接字。。。");
		int port = Integer.valueOf(PropertyPlaceholderConfigurerProvider.getContextProperty("server.taskPort"))
		String ipAddr = PropertyPlaceholderConfigurerProvider.getContextProperty("server.taskAddress")
		ServerSocket serverSocket = new ServerSocket(port,10,InetAddress.getByName(ipAddr))
		while(flag) {
			Socket s = serverSocket.accept();
			System.out.println("a client connect!");
			DataInputStream dis = new DataInputStream(s.getInputStream());
			String command = dis.readUTF()
			System.out.println(command);
			DataOutputStream dout = new DataOutputStream(s.getOutputStream());
			if(command.equalsIgnoreCase("stop")){
				flag = false
			}else{
				dout.writeUTF("参数不正确！")
				Thread.sleep(2000);
				dis.close();
				dout.close()
				s.close();
				continue
			}

			dout.writeUTF("服务正在退出。。。")
			dis.close();
			dout.close()
			s.close();
		}
		serverSocket.close()
		ServerDestroyProcess serverDestroyProcess = SpringContextProvider.getBean("serverDestroyProcess");
		serverDestroyProcess.stop();
	}

	protected ArrayList<?> displayStatus(String command) {
		// TODO Auto-generated method stub
		return null;
	}

	protected boolean preInitialize() {
		String rootPath = System.getenv("MYSTOPE_ROOTPATH")
		println rootPath
		if(rootPath == null){
			rootPath="E:\\workspace2\\GreatDataCollector"
		}
		ServerPath serverPath = JaxbUtil.converyFileToJavaBean(args[1], "UTF-8", ServerPath.class);
		if(rootPath != null && !rootPath.equals("")){
			serverPath.rootPath = rootPath
		}
		serverPath.taskConfigPath.taskPath.each {
			println it
		}
		serverPath.reInit()
		ServerContext.serverPath = serverPath;
		if (serverPath.getBaseConfigPath() == null) {
			logger.error("服务初始化文件路径不对。");
			return false;
		}
		return true;
	}

	protected void printUsage() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		logger.debug("初始化参数" + args)
		if (args.length == 2) {
			if (args[0].equalsIgnoreCase("start")) {
				Launcher app = new Launcher();
				app.setArgs(args);
				app.run();
				logger.debug("luncher 正在退出......");
				System.exit(1);
			}else if(args[0].equalsIgnoreCase("stop")){
				Launcher app = new Launcher();
				app.setArgs(args)
				if(!app.preInitialize()){
					logger.debug("程序引擎初始化失败！")
					return;
				}
				PropertyPlaceholderConfigurerProvider.init()
				PropertyPlaceholderConfigurerProvider.loadProperties()
				String ipAddr = PropertyPlaceholderConfigurerProvider.getContextProperty("server.taskAddress")
				int port = Integer.valueOf(PropertyPlaceholderConfigurerProvider.getContextProperty("server.taskPort"))
				println("IP:"+ipAddr+"--PORT:"+port)
				Socket socket = new Socket(ipAddr,port)
				while(true){
					DataOutputStream dout = new DataOutputStream(socket.getOutputStream())
					dout.writeUTF("stop")
					InputStream is = socket.getInputStream();
					DataInputStream dis = new DataInputStream(is);
					System.out.println(dis.readUTF());
					dout.close()
					dis.close();
					break
				}
				socket.close();
			}
		}
	}
}
